﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DTO;
using DAO;
using BUS;

namespace GUI
{
    public partial class Phiếuphat : UserControl
    {
        public Phiếuphat()
        {
            InitializeComponent();
        }
        
        DataTable dt = new DataTable();
        DataTable dd = new DataTable();
        DataSet ds = new DataSet();
        int irach = 10000;
        int itre = 5000;
        int imat = 50000;
        private int m_iTong = 0;
        int aa = 0;
        int bb = 0;
        int cc = 0;

        private void Phiếuphat_Load(object sender, EventArgs e)
        {
            cbo();
            txtthanhtien.Enabled = false;

           dt=DataProvider.GetDataTable("Docgia",DataProvider.ketnoi());
            cbomaDG.DisplayMember = "MaDG";
            cbomaDG.ValueMember = "MaDG";
            cbomaDG.DataSource = dt;

            txtmaphieuphat.Enabled = false;
            txtmaphieuphat.Text = PhieuphatDAO.laymasach().ToString();

            dd = DataProvider.GetDataTable("Phieuphat", DataProvider.ketnoi());

            Loadlistview(dd, lvwphieuphat);
        }
        #region cbo
        private void cbo()
        {
            cbo3.Items.Add("1");
            cbo2.Items.Add("1");
            cbo1.Items.Add("1");
            cbo1.Items.Add("2");
            cbo1.Items.Add("3");
            cbo2.Items.Add("2");
            cbo2.Items.Add("3");
            cbo3.Items.Add("2");
            cbo3.Items.Add("3");
        }
        #endregion  

         void Loadlistview(DataTable data, ListView lv)
        {
            lvwphieuphat.Items.Clear();
            int istt = 0;
            foreach (DataRow dr in dd.Rows)
            {
                istt++;
                ListViewItem lvw = new ListViewItem();
                lvw.Text = istt.ToString();
                lvw.SubItems.Add(dr["MaPP"].ToString());
                lvw.SubItems.Add(dr["MaDG"].ToString());
                lvw.SubItems.Add(dr["TenDG"].ToString());
                lvw.SubItems.Add(dr["Lydo"].ToString());
                lvw.SubItems.Add(dr["Thanhtien"].ToString());
                lvw.ImageIndex = 0;
                lvwphieuphat.Items.Add(lvw);
            }

        }

        private void lvwphieuphat_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lvwphieuphat.SelectedItems.Count > 0)
            {
                ListViewItem lvw = lvwphieuphat.SelectedItems[lvwphieuphat.SelectedItems.Count - 1];
                if (lvw != null)
                {
                    foreach (ListViewItem lv in lvwphieuphat.SelectedItems)
                    {
                        txtmaphieuphat.Text = lv.SubItems[1].Text;
                        cbomaDG.Text = lv.SubItems[2].Text;
                        txttenDG.Text = lv.SubItems[3].Text;
                        txtlydo.Text = lv.SubItems[4].Text;
                        txtthanhtien.Text = lv.SubItems[5].Text;
                    }
                }
            }
        }

        private void cbomaDG_SelectedIndexChanged(object sender, EventArgs e)
        {
            string strmaDG;
            strmaDG = cbomaDG.SelectedValue.ToString();
            DataTable data = new DataTable();
            dt = DataProvider.laythongtindocgia(strmaDG, DataProvider.ketnoi());
            DataRow dr;
            dr = dt.Rows[0];
            txttenDG.Text = dr["TenDG"].ToString();
        }


        #region tinhtien
        private void TinhTien()
        {
            int a = 0;
            int b = 0;
            int c = 0;
            if (cbo1.SelectedIndex != -1)
            {
                a = Convert.ToInt32(cbo1.Text);
                 aa = (a * irach);
                //m_iTong += aa;
                 tong();
            }

            if (cbo2.SelectedIndex != -1)
            {
                b = Convert.ToInt32(cbo2.Text);
                bb = (b * itre);
                tong();
            }
            if (cbo3.SelectedIndex != -1)
            {
                c = Convert.ToInt32(cbo3.Text);
                cc = (c * imat);
                tong();
            }
                
            else
            {
                txtthanhtien.Text = "0";
            }

            txtthanhtien.Text = m_iTong.ToString();
        }
        #endregion

        void tong()
        {


            m_iTong = aa + bb + cc;
        }

        private void cbo1_SelectedIndexChanged(object sender, EventArgs e)
        {
            TinhTien();
        }

        private void cbo2_SelectedIndexChanged(object sender, EventArgs e)
        {
            TinhTien();
        }

        private void cbo3_SelectedIndexChanged(object sender, EventArgs e)
        {
            TinhTien();
        }

        private void btnlapphieu_Click(object sender, EventArgs e)
        {
            DialogResult d = MessageBox.Show("Bạn có chắc là muốn mượn sách này ko ?", "Thông Báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);
            if (d == DialogResult.Yes)
            {
                PhieuphatDTO dto = new PhieuphatDTO();
                dto.MaDG = cbomaDG.Text;
                dto.TenDG = txttenDG.Text;
                dto.Lydo = txtlydo.Text;
                dto.Thanhtien = Convert.ToInt32(txtthanhtien.Text);
                if (PhieuphatBUS.lapphieuphat(dto) == true)
                {
                    MessageBox.Show("Lập phiếu phạt thành công", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    Loadlistview(dd, lvwphieuphat);
                }
            }
        }

        private void btnupdate_Click(object sender, EventArgs e)
        {
            cbomaDG.Focus();
            lvwphieuphat.Enabled = false;
            btnupdate.Visible = false;
            btnluu.Visible = true;
        }

        private void btnluu_Click(object sender, EventArgs e)
        {
            btnupdate.Visible = true;
            btnluu.Visible = false;
            lvwphieuphat.Enabled = true;
            DialogResult d = MessageBox.Show("Bạn có chắc là muốn update  ko ?", "Thông Báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);
            if (d == DialogResult.Yes)
            {
                PhieuphatDTO dto = new PhieuphatDTO();
                dto.MaPP = Convert.ToInt32(txtmaphieuphat.Text);
                dto.MaDG = cbomaDG.Text;
                dto.TenDG = txttenDG.Text;

                dto.Lydo = txtlydo.Text;
                dto.Thanhtien = Convert.ToInt32(txtthanhtien.Text);
               
                if (PhieuphatBUS.updatephieuphat(dto) == true)
                {
                    MessageBox.Show("Update phiếu phạt thành công", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    Phiếuphat_Load(sender, e);
                }
            }
        }

        private void btnxoa_Click(object sender, EventArgs e)
        {
            DialogResult d = MessageBox.Show("Bạn có chắc là muốn update  ko ?", "Thông Báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);
            if (d == DialogResult.Yes)
            {
                PhieuphatDTO dto = new PhieuphatDTO();
                dto.MaPP = Convert.ToInt32(txtmaphieuphat.Text);
                if (PhieuphatDAO.xoaPP(dto) == true)
                {
                    MessageBox.Show("Xoá phiếu phạt thành công", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    Phiếuphat_Load(sender, e);
                }
            }

        }

        private void btnthoat_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }



    }
}
